package priv.blackbibo.application.service;


import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.Vibrator;
import android.util.Log;

import androidx.annotation.Nullable;

import priv.blackbibo.application.R;
import priv.blackbibo.application.activity.MainActivity;

public class MediaPlaybackService extends Service {
    private MediaPlayer mediaPlayer;

    private static final String TAG = "ms";

    private Boolean isStop = true;

    private Vibrator vibrator;

    public MediaPlaybackService() {
        Log.d(TAG, "MediaPlaybackService: init");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    //在此方法中服务被创建
    @Override
    public void onCreate() {
        super.onCreate();
        if (mediaPlayer==null){
            mediaPlayer=new MediaPlayer();

            //为播放器添加播放完成时的监听器
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    //发送广播到MainActivity
                    Intent intent=new Intent();
                    intent.setAction("com.complete");
                    sendBroadcast(intent);
                }
            });
        }

        vibrator = (Vibrator)this.getSystemService(this.VIBRATOR_SERVICE);
    }


    /**
     * 在此方法中，可以执行相关逻辑，如耗时操作
     * @param intent :由Activity传递给service的信息，存在intent中
     * @param flags ：规定的额外信息
     * @param startId ：开启服务时，如果有规定id，则传入startid
     * @return 返回值规定此startservice是哪种类型，粘性的还是非粘性的
     *          START_STICKY:粘性的，遇到异常停止后重新启动，并且intent=null
     *          START_NOT_STICKY:非粘性，遇到异常停止不会重启
     *          START_REDELIVER_INTENT:粘性的，重新启动，并且将Context传递的信息intent传递
     * 此方法是唯一的可以执行很多次的方法
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        synchronized (isStop){
            switch (intent.getIntExtra("type",-1)){
                case MainActivity.PLAT_MUSIC:
                if (isStop){
                    //重置mediaplayer
                    Log.d(TAG, "onStartCommand: play start");
                    if (mediaPlayer.isPlaying()){
                        mediaPlayer.stop();
                        mediaPlayer.release();
                    }
                    mediaPlayer.reset();
                    //将需要播放的资源与之绑定
                    mediaPlayer=MediaPlayer.create(this, R.raw.heshuisheng);
                    //开始播放
                    mediaPlayer.start();
                    //是否循环播放
                    mediaPlayer.setLooping(true);
                    isStop=false;
                    int di = 1200;
//                    long[] patter = {di,2*di,6*di,di,3*di ,di,3*di};
                    long[] patter = {di,di};
                    vibrator.vibrate(patter, 0);
                }else if (!isStop&&mediaPlayer.isPlaying()&&mediaPlayer!=null){
                    Log.d(TAG, "onStartCommand: replay");
                    mediaPlayer.start();
                }
                    break;
                case MainActivity.PAUSE_MUSIC:
                    //播放器不为空，并且正在播放
                    if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
                        mediaPlayer.pause();
                    }
                    break;
                case MainActivity.STOP_MUSIC:
                    if (mediaPlayer!=null){
                        //停止之后要开始播放音乐
                        mediaPlayer.stop();
                        isStop=true;
                    }
                    vibrator.cancel();
                    break;
            }
        }

        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}